package com.hktf.uilib.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorChangedListener;
import com.flask.colorpicker.OnColorSelectedListener;
import com.flask.colorpicker.slider.AlphaSlider;
import com.flask.colorpicker.slider.LightnessSlider;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.bean.FontItem;
import com.hktf.uilib.bean.KeyValueItem;
import com.hktf.uilib.R;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PickColorPopWindow extends PopupWindow{
    private int color=-1;
    private Integer[] initialColor = new Integer[]{null, null, null, null, null};

    public PickColorPopWindow(final BaseActivity context,int initColor, MyListener listener) {
        this.myListener=listener;
        View rootView=View.inflate(context, R.layout.fragment_adapted_dialog,null);

        ColorPickerView colorPickerView=rootView.findViewById(R.id.color_picker_view);
        colorPickerView.setShowBorder(true);
        LightnessSlider lightnessSlider=rootView.findViewById(R.id.v_lightness_slider);
        AlphaSlider alphaSlider=rootView.findViewById(R.id.v_alpha_slider);
        colorPickerView.setAlphaSlider(alphaSlider);
        colorPickerView.setLightnessSlider(lightnessSlider);
        color=initColor;
        initialColor[0]=color;
        colorPickerView.setInitialColors(initialColor,getStartOffset(initialColor));
        lightnessSlider.setColor(getStartColor(initialColor));
        alphaSlider.setColor(getStartColor(initialColor));
//        colorPickerView.setAlphaValue(1);
//        colorPickerView.setLightness(1);
        colorPickerView.addOnColorSelectedListener(new OnColorSelectedListener() {
            @Override
            public void onColorSelected(int selectedColor) {
                color=selectedColor;
                if (myListener != null) {
                    myListener.onPickColor(color);
                }
            }
        });
        colorPickerView.addOnColorChangedListener(new OnColorChangedListener() {
            @Override
            public void onColorChanged(int selectedColor) {
                color=selectedColor;
                if (myListener != null) {
                    myListener.onPickColor(color);
                }
            }
        });
        rootView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rootView.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

            }
        });
        setContentView(rootView);
        setOutsideTouchable(true);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
    }
    private Integer getStartOffset(Integer[] colors) {
        Integer start = 0;
        for (int i = 0; i < colors.length; i++) {
            if (colors[i] == null) {
                return start;
            }
            start = (i + 1) / 2;
        }
        return start;
    }
    private int getStartColor(Integer[] colors) {
        Integer startColor = getStartOffset(colors);
        return startColor == null ? Color.WHITE : colors[startColor];
    }


    private MyListener myListener;

    public MyListener getMyListener() {
        return myListener;
    }

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }

    public static interface MyListener{
        void onPickColor(int color);
    }

    private OnCheckChangeListener onCheckChangeListener;

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public static interface OnCheckChangeListener{
        void onCheckChange(KeyValueItem keyValueItem,int position);
    }

}
